package com.starblink.android.basic.sensorsdata.spm.impl

import com.starblink.android.basic.sensorsdata.spm.impl.SPMStore.Companion.A
import com.starblink.android.basic.sensorsdata.spm.impl.SPMStore.Companion.B
import java.util.Random

class SPMStore {
//    import com.starblink.android.basic.ext.guard

    /**
     *
    {

    "trackUniqueId":"1676255869047_dsjxi",
    "env":0,
    "clientVersion":"0.8.0",
    "clientBuild":"111",
    "bundleId":"com.starblink.android.guang",
    "os":"and",
    "osVersion":"29",
    "networkType":"wifi",
    "model":"SM-G960U",
    "brand":"samsung",
    "country":"Hans",
    "language":"zh-Hans-CN",
    "tz":"Asia/Shanghai",
    "ts":"1676255869047",
    "deviceId":"and_d44b2ff0-96f3-11ed-a7a5-110713d88f2d",
    "timeSinceLaunch":"757",
    "idfa":"",
    "androidId":"52e68c27-2e06-4d2e-8a8a-25a7bbb3c48b",
    "isOpenPush":"",
    "launchType":"none",
    "sessionId":"f13f6786-89a6-5e27-bb4a-c964291801fb",
    "userId":"",
    "event":"0",
    "eventParams":{
    "parent":"HomeRoute",
    "sk_tracker_count":"3"
    },
    "eventPath":[
    ],
    "route":"Home080Page_10164",
    "spmCnt":"s0.ca.10164.0.a2b877oep",
    "spmPrev":null,
    "traceInfo":null,
    "params":{
    }

    }

     *
     */
    companion object {
        const val A = "s0"
        const val B = "ca"
        const val letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    }


    //generateUniqueRandomStringInThisLaunch
    fun randomString(): String {
        val random1 = Random()
        //指定字符串长度，拼接字符并toString
        //指定字符串长度，拼接字符并toString
        val sb = StringBuffer()
        for (i in 0..7) {
            //获取指定长度的字符串中任意一个字符的索引值
            val number = random1.nextInt(letters.length)
            //根据索引值获取对应的字符
            val charAt: Char = letters[number]
            sb.append(charAt)
        }
        return sb.toString()
    }

    //不需要了 读json维护性比较差

//    var pageMap: Map<String, Int>? = null
//    var elementMap: Map<String, Int>? = null
//    var statisticsMap: Map<String, Int>? = null
//
//    fun setup() {
//        this.pageMap = getJsonConfig("spm_cfg/spm_page_def.json")
//        this.elementMap = getJsonConfig("spm_cfg/spm_page_def.json")
//        this.statisticsMap = getJsonConfig("spm_cfg/spm_statistics_def.json")
//    }
//
//    private fun getJsonConfig(jsonName: String): Map<String, Int>? {
//        //读本地json配置
//        val netCfgJsonStr =
//            BaseApiConfig.readText4Assets(CommUtils.getContext(), jsonName)
//        var spmMap: Map<String, Int>? = null
//        if (netCfgJsonStr != null) {
//            //JSON转map
//            spmMap = Gson().fromJson<Map<String, Int>>(netCfgJsonStr, Map::class.java)
//        }
//        return spmMap
//    }

}

fun SPMStore.fetchPageValue(page: Int): String {
    val values = arrayOf(A, B, page, "0", randomString())
    return values.joinToString(".")
}

fun SPMStore.fetchElementValue(page: Int, name: Int): String {
    val values = arrayOf(A, B, page, name, randomString())
    return values.joinToString(".")
}

fun SPMStore.fetchStatistics(page: Int, name: Int): String {
    val values = arrayOf(A, B, page, name, randomString())
    return values.joinToString(".")
}








